package com.bsuir.avareader.logic;

import android.content.Context;
import android.database.Cursor;
import android.widget.TextView;

import com.bsuir.avareader.dal.AvaReaderDbHelper;
import com.bsuir.avareader.dal.TextbookDbAdapter;

public class BookManager {
	private TextbookDbAdapter textbookAdapter;
	
	public BookManager(Context context) {
		textbookAdapter = new TextbookDbAdapter(context);
	}
	
	public String getScreenText(TextView tv, int startLine) {
		int curLine = startLine;
		StringBuilder text = new StringBuilder("");
		
		for (int i = startLine; i < startLine + this.getLinesCount(tv); i++) {
			Cursor cursor = textbookAdapter.fetchLine(curLine);
			int colIndex = cursor.getColumnIndex(AvaReaderDbHelper.LINE);
			if (cursor.moveToFirst()) {
				text.append(cursor.getString(colIndex));
			}
		}	
		return text.toString();
	}
	
	private int getLinesCount(TextView tv) {
		return tv.getHeight() / tv.getLineHeight();
	}
}
